1   /*
2    * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6842557 6943963 6959267
27   * @summary confirm that shaping works as expected. (Mainly for new characters which were added in Unicode 5 and 6)
28   * used where appropriate.
29   */
30  
31  import java.awt.font.NumericShaper;
32  import java.util.EnumSet;
33  import static java.awt.font.NumericShaper.*;
34  
35  public class ShapingTest {
36  
37      private static boolean err = false;
38  
39      public static void main(String[] args) {
40          test6842557();
41          test6943963();
42          test6903266();
43  
44          if (err) {
45              throw new RuntimeException("shape() returned unexpected value.");
46          }
47      }
48  
49      private static void test6842557() {
50          NumericShaper ns_old = getContextualShaper(ARABIC | TAMIL | ETHIOPIC,
51                                     EUROPEAN);
52          NumericShaper ns_new = getContextualShaper(EnumSet.of(
53                                     Range.ARABIC, Range.TAMIL, Range.ETHIOPIC),
54                                     Range.EUROPEAN);
55  
56          String[][] data = {
57             // Arabic "October 10"
58            {"\u0623\u0643\u062a\u0648\u0628\u0631 10",
59             "\u0623\u0643\u062a\u0648\u0628\u0631 \u0661\u0660"},
60  
61             // Tamil "Year 2009"
62            {"\u0b86\u0ba3\u0bcd\u0b9f\u0bc1 2009",
63             "\u0b86\u0ba3\u0bcd\u0b9f\u0bc1 \u0be8\u0be6\u0be6\u0bef"},
64             // "\u0be800\u0bef is returned by pre-JDK7 because Tamil zero was not
65             //  included in Unicode 4.0.0.
66  
67             // Ethiopic "Syllable<HA> 2009"
68            {"\u1200 2009",
69             "\u1200 \u136a00\u1371"},
70             // Ethiopic zero doesn't exist even in Unicode 5.1.0.
71          };
72  
73          for (int i = 0; i < data.length; i++) {
74              checkResult("ARABIC | TAMIL | ETHIOPIC",
75                          ns_old, data[i][0], data[i][1]);
76  
77              checkResult("Range.ARABIC, Range.TAMIL, Range.ETHIOPIC",
78                          ns_new, data[i][0], data[i][1]);
79          }
80      }
81  
82      private static void test6943963() {
83          // Needed to reproduce this bug.
84          NumericShaper ns_dummy = getContextualShaper(ARABIC | TAMIL | ETHIOPIC,
85                                     EUROPEAN);
86          char[] c = "\u1200 1".toCharArray();
87          ns_dummy.shape(c, 0, c.length);
88  
89  
90          String given = "\u0627\u0628 456";
91          String expected_ARABIC = "\u0627\u0628 \u0664\u0665\u0666";
92          String expected_EASTERN_ARABIC = "\u0627\u0628 \u06f4\u06f5\u06f6";
93  
94          NumericShaper ns = getContextualShaper(ARABIC);
95          checkResult("ARABIC", ns, given, expected_ARABIC);
96  
97          ns = getContextualShaper(EnumSet.of(Range.ARABIC));
98          checkResult("Range.ARABIC", ns, given, expected_ARABIC);
99  
100         ns = getContextualShaper(EASTERN_ARABIC);
101         checkResult("EASTERN_ARABIC", ns, given, expected_EASTERN_ARABIC);
102 
103         ns = getContextualShaper(EnumSet.of(Range.EASTERN_ARABIC));
104         checkResult("Range.EASTERN_ARABIC", ns, given, expected_EASTERN_ARABIC);
105 
106         ns = getContextualShaper(ARABIC | EASTERN_ARABIC);
107         checkResult("ARABIC | EASTERN_ARABIC", ns, given, expected_EASTERN_ARABIC);
108 
109         ns = getContextualShaper(EnumSet.of(Range.ARABIC, Range.EASTERN_ARABIC));
110         checkResult("Range.ARABIC, Range.EASTERN_ARABIC", ns, given, expected_EASTERN_ARABIC);
111     }
112 
113     private static void test6903266() {
114         NumericShaper ns = getContextualShaper(EnumSet.of(Range.TAI_THAM_HORA));
115         String given = "\u1a20 012";
116         String expected = "\u1a20 \u1a80\u1a81\u1a82";
117         checkResult("Range.TAI_THAM_HORA", ns, given, expected);
118 
119         ns = getContextualShaper(EnumSet.of(Range.TAI_THAM_HORA,
120                                             Range.TAI_THAM_THAM));
121         given = "\u1a20 012";
122         expected = "\u1a20 \u1a90\u1a91\u1a92"; // Tham digits are prioritized.
123         checkResult("Range.TAI_THAM_HORA, Range.TAI_THAM_THAM", ns, given, expected);
124 
125         ns = getContextualShaper(EnumSet.of(Range.JAVANESE));
126         given = "\ua984 012";
127         expected = "\ua984 \ua9d0\ua9d1\ua9d2";
128         checkResult("Range.JAVANESE", ns, given, expected);
129 
130         ns = getContextualShaper(EnumSet.of(Range.TAI_THAM_THAM));
131         given = "\u1a20 012";
132         expected = "\u1a20 \u1a90\u1a91\u1a92";
133         checkResult("Range.TAI_THAM_THAM", ns, given, expected);
134 
135         ns = getContextualShaper(EnumSet.of(Range.MEETEI_MAYEK));
136         given = "\uabc0 012";
137         expected = "\uabc0 \uabf0\uabf1\uabf2";
138         checkResult("Range.MEETEI_MAYEK", ns, given, expected);
139     }
140 
141     private static void checkResult(String ranges, NumericShaper ns,
142                                     String given, String expected) {
143         char[] text = given.toCharArray();
144         ns.shape(text, 0, text.length);
145         String got = new String(text);
146 
147         if (!expected.equals(got)) {
148             err = true;
149             System.err.println("Error with range(s) <" + ranges + ">.");
150             System.err.println("  text     = " + given);
151             System.err.println("  got      = " + got);
152             System.err.println("  expected = " + expected);
153         } else {
154             System.out.println("OK with range(s) <" + ranges + ">.");
155             System.out.println("  text     = " + given);
156             System.out.println("  got      = " + got);
157             System.out.println("  expected = " + expected);
158         }
159     }
160 
161 }